package com.ysy.tz.core.image;

import com.google.zxing.common.BitMatrix;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;

public class MatrixImage extends WritableImage {
    private BitMatrix bitMatrix;
    private Color darkColor;
    private Color lightColor;

    public MatrixImage(BitMatrix bitMatrix) {
        super(bitMatrix.getWidth(), bitMatrix.getHeight());
        this.bitMatrix = bitMatrix;
        this.darkColor = Color.BLACK;
        this.lightColor = Color.WHITE;
    }

    public MatrixImage(BitMatrix bitMatrix, Color darkColor, Color lightColor) {
        super(bitMatrix.getWidth(), bitMatrix.getHeight());
        this.bitMatrix = bitMatrix;
        this.darkColor = darkColor;
        this.lightColor = lightColor;
    }

    public WritableImage getImage() {
        PixelWriter pw = this.getPixelWriter();

        for(int x = 0; x < this.bitMatrix.getWidth(); ++x) {
            for(int y = 0; y < this.bitMatrix.getHeight(); ++y) {
                if (this.bitMatrix.get(x, y)) {
                    pw.setColor(x, y, this.darkColor);
                } else {
                    pw.setColor(x, y, this.lightColor);
                }
            }
        }

        return (WritableImage)(new ImageView(this)).getImage();
    }
}
